package com.cmcc.driver.ui.profile

import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.alibaba.android.arouter.facade.annotation.Route
import com.cmcc.driver.R
import com.cmcc.driver.config.DriverPath
import com.cmcc.driver.databinding.ActivityFeedbackBinding
import com.cmcc.driver.ui.base.BasicActivity
import com.cmcc.driver.ui.dialog.AppAlertDialog
import com.cmcc.driver.ui.dialog.AppProgressDialog
import com.cmcc.driver.ui.profile.viewModel.FeedbackViewModel

@Route(path = DriverPath.FEED_BACK)
class FeedbackActivity : BasicActivity() {

    private lateinit var binding: ActivityFeedbackBinding
    private var viewModel: FeedbackViewModel? = null

    private var progressDialog: AppProgressDialog? = null

    override fun initialize(savedInstanceState: Bundle?) {
        binding = DataBindingUtil.setContentView<ActivityFeedbackBinding>(
            this,
            R.layout.activity_feedback
        )
        binding.appbar.toolbar.setNavigationOnClickListener { finish() }
        binding.appbar.title.text = getString(R.string.feedback)

        viewModel = ViewModelProviders.of(this).get(FeedbackViewModel::class.java)

        progressDialog = AppProgressDialog(this)
    }

    override fun initView(savedInstanceState: Bundle?) {

        binding.viewModel = viewModel
    }

    override fun initEvent(savedInstanceState: Bundle?) {
        viewModel?._confirmSelector?.observe(this, Observer {

            val content = binding.content.text.toString()

            if(content.isNullOrEmpty()){
                AppAlertDialog(this).setContent(getString(R.string.feedback_empty_alert))
                    .setNavigation("", null, false)
                    .setPositive(getString(R.string.make_sure), View.OnClickListener {  })
                    .show()
            }else{
                progressDialog?.show()
                viewModel?.feedback(content)
            }


        })


    }

    override fun initObserver(savedInstanceState: Bundle?) {
        viewModel?.complaintRecord?.observe(this, Observer { code ->
            progressDialog?.dismiss()
            if (code == 200) {
                AppAlertDialog(this).setContent(getString(R.string.feedback_success))
                    .setNavigation("", null, false)
                    .setPositive(getString(R.string.make_sure), View.OnClickListener { finish() })
                    .show()
            }
        })
    }

}